package com.moli.iov.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.base.cache.redis.VehicleTboxCacheService;
import com.moli.iov.base.mapper.VehicleTboxMapper;
import com.moli.iov.base.model.VehicleTboxParam;
import com.moli.iov.base.model.dto.req.QueryVehicleTboxListParamDto;
import com.moli.iov.base.model.dto.req.VehicleTboxReqDto;
import com.moli.iov.base.model.po.VehicleTbox;
import com.moli.iov.base.service.IVehicleTboxService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import com.moli.iov.util.ValidationUtils;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 车辆基tbox表  服务实现类
 * </p>
 *
 * @author wanghualin
 * @since 2019-09-02
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class VehicleTboxServiceImpl extends ServiceImpl<VehicleTboxMapper, VehicleTbox> implements
    IVehicleTboxService {

  @Autowired
  private VehicleTboxCacheService vehicleTboxCacheService;

  @Resource
  private VehicleTboxMapper vehicleTboxMapper;

  @Override
  public RestResponse saveVehicleTbox(VehicleTboxReqDto vehicleTboxReqDto) {
    log.info("添加VehicleTboxReqDto：{}" + vehicleTboxReqDto);
    if (vehicleTboxReqDto == null) {
      return RestResponse.failure("参数为null");
    }
    VehicleTbox vehicleTbox = new VehicleTbox();
    CommonUtils.copyProperties(vehicleTboxReqDto, vehicleTbox);
    vehicleTbox.setTboxVersion("1");
    vehicleTbox.setCreateUser(vehicleTboxReqDto.getSysUserId().toString());
    vehicleTbox.setGmtCreate(Calendar.getInstance().getTime());
    vehicleTbox.setVin("");
    boolean save = this.save(vehicleTbox);
    if (save) {
      vehicleTboxCacheService.setVehicleTbox(vehicleTbox);
    }
    return save ? RestResponse.success() : RestResponse.failure("添加失败");
  }

  @Override
  public boolean updateVehicleTbox(VehicleTbox oldVehicleTbox, VehicleTbox updateVehicleTbox) {
    ValidationUtils.validateBean(oldVehicleTbox);
    ValidationUtils.validateBean(updateVehicleTbox);
    //淘汰缓存
    vehicleTboxCacheService.deleteVehicleTbox(oldVehicleTbox);
    boolean save = this.saveOrUpdate(updateVehicleTbox);
    if (save) {
      vehicleTboxCacheService.setVehicleTbox(updateVehicleTbox);
    }
    return save;
  }

  @Override
  public VehicleTbox getVehicleTboxByVin(String vin) {
    if (StringUtils.isBlank(vin)) {
      throw new BusinessException("vin：参数为空");
    }
    VehicleTbox vehicleTboxByVin = vehicleTboxCacheService.getVehicleTboxByVin(vin);
    if (vehicleTboxByVin != null) {
      return vehicleTboxByVin;
    } else {
      VehicleTboxParam vehicleTboxParam = new VehicleTboxParam();
      vehicleTboxParam.queryEqVin(vin);
      return this.getOne(vehicleTboxParam.get(), false);
    }
  }

  /**
   * 抛出
   */
  @Override
  public VehicleTbox getVehicleTboxByDeviceId(String deviceId) {
    if (StringUtils.isBlank(deviceId)) {
      throw new BusinessException("vin：参数为空");
    }
    VehicleTbox vehicleTboxByVin = vehicleTboxCacheService.getVehicleTboxByDeviceId(deviceId);
    if (vehicleTboxByVin != null) {
      return vehicleTboxByVin;
    } else {
      VehicleTboxParam vehicleTboxParam = new VehicleTboxParam();
      vehicleTboxParam.queryEqDeviceId(deviceId);
      VehicleTbox one = this.getOne(vehicleTboxParam.get(), false);
      if (one != null) {
        vehicleTboxCacheService.setVehicleTbox(one);
      }
      return one;
    }
  }

  @Override
  public IPage<VehicleTbox> listVehicleTboxs(
      QueryVehicleTboxListParamDto queryParam) {
    ValidationUtils.validateBean(queryParam);
    VehicleTboxParam vehicleTboxParam = getVehicleTboxParamByVehicleTbox(queryParam);
    QueryWrapper<VehicleTbox> vehicleTboxQueryWrapper = vehicleTboxParam.get();
    vehicleTboxQueryWrapper.orderByDesc("gmt_create");
    IPage<VehicleTbox> page = new Page(queryParam.getPageNum(), queryParam.getPageSize());
    IPage<VehicleTbox> list = super.page(page, vehicleTboxQueryWrapper);
    return list;
  }

  @Override
  public List<VehicleTbox> listVehicleTbox() {
    List<VehicleTbox> vehicleTbox = vehicleTboxCacheService.listVehicleTbox();
    if (vehicleTbox != null && !vehicleTbox.isEmpty()) {
      return vehicleTbox;
    }
    List<VehicleTbox> list = this.list(new VehicleTboxParam().get());
    if (list != null && !list.isEmpty()) {
      vehicleTboxCacheService.saveVehicleTboxs(list);
      return list;
    }
    return new ArrayList<>();
  }

  @Override
  public Map<String, String> listVehicleTboxMap() {
    Map<String, String> map = vehicleTboxCacheService.listVehicleTboxMap();
    if (map != null && !map.isEmpty()) {
      return map;
    } else {
      List<VehicleTbox> list = this.list(new VehicleTboxParam().get());
      if (list != null && !list.isEmpty()) {
        vehicleTboxCacheService.saveVehicleTboxs(list);
        return vehicleTboxCacheService.listVehicleTboxMap();
      }
    }
    return null;
  }

  @Override
  public Map<String, String> listVehicleTboxModelMap() {
    Map<String, String> maps = vehicleTboxCacheService.listVehicleTboxModelMap();
    if (maps != null && !maps.isEmpty()) {
      return maps;
    } else {
      List<VehicleTbox> list = this.list(new VehicleTboxParam().get());
      if (list != null && !list.isEmpty()) {
        vehicleTboxCacheService.saveVehicleTboxs(list);
        return vehicleTboxCacheService.listVehicleTboxModelMap();
      }
    }
    return null;
  }

  @Override
  public VehicleTbox getVehicleTboxBySimNo(String simNo) {
    VehicleTboxParam vehicleTboxParam = new VehicleTboxParam();
    vehicleTboxParam.queryEqSimNo(simNo);
    return getOne(vehicleTboxParam.get(), false);
  }

  @Override
  public void initVehicleTbox() {
    //删除缓存
    vehicleTboxCacheService.initCache();
    VehicleTboxParam param = new VehicleTboxParam();
    List<VehicleTbox> list = list(param.get());
    if (!CollectionUtils.isEmpty(list)) {
      vehicleTboxCacheService.saveVehicleTboxs(list);
    }
  }

  @Override
  public List<VehicleTbox> listVehicleTboxList(List<String> vinList) {
    ValidationUtils.validateBean(vinList);
    List<VehicleTbox> tboxList = vehicleTboxCacheService.listVehicleTboxByVin(vinList);
    if (CollectionUtils.isEmpty(tboxList)) {
      vehicleTboxCacheService.listVehicleTboxModelMap();
      ValidationUtils.validateBean(vinList);
      VehicleTboxParam vehicleTboxParam = new VehicleTboxParam();
      vehicleTboxParam.queryInVin(vinList);
      List<VehicleTbox> list = this.list(vehicleTboxParam.get());
      if (!CollectionUtils.isEmpty(list)) {
        vehicleTboxCacheService.saveVehicleTboxs(list);
      }
      return list;
    } else {
      return tboxList;
    }
  }

  @Override
  public RestResponse queryVehicleTboxCount() {
    log.info("查询在网数");
    Long allCount = vehicleTboxCacheService.getAllSize();
    if (allCount == null || allCount < 1) {
      VehicleTboxParam vehicleTboxParam = new VehicleTboxParam();
      int count = super.count(vehicleTboxParam.get());
      return RestResponse.success(count);
    } else {
      return RestResponse.success(allCount);
    }
  }
  /**
   *
   * 设备管理> 终端安装管理-有效授权列表
   * @param queryParam
   * @version  3.10
   * @return
   */
  @Override
  public IPage<VehicleTbox> listVehicleTboxBinding(QueryVehicleTboxListParamDto queryParam) {

    ValidationUtils.validateBean(queryParam);

    VehicleTbox vehicleTbox = getVehicleTboxBindingParam(queryParam);

    Page<VehicleTbox> page = new Page(queryParam.getPageNum(), queryParam.getPageSize());

    return vehicleTboxMapper.listVehicleTboxBindingPage(page, vehicleTbox);
  }

  @Override
  public VehicleTbox getVehicleTboxByIccid(String iccid) {
    VehicleTbox vehicleTbox = vehicleTboxCacheService.getVehicleTboxByIccid(iccid);
    if (vehicleTbox != null) {
      return vehicleTbox;
    } else {
      VehicleTboxParam vehicleTboxParam = new VehicleTboxParam();
      vehicleTboxParam.queryEqIccid(iccid);
      return this.getOne(vehicleTboxParam.get(), false);
    }
  }


  /**
   * 查询封装
   */
  private VehicleTboxParam getVehicleTboxParamByVehicleTbox(
      QueryVehicleTboxListParamDto queryParam) {
    VehicleTboxParam vehicleTboxParam = new VehicleTboxParam();
    vehicleTboxParam
        .queryEqVin(queryParam.getVin())
        .queryEqSupplierName(queryParam.getSupplierName())
        .queryEqDeviceModel(queryParam.getDeviceModel())
        .queryBetweenRegisterTime(queryParam.getStartTime(), queryParam.getEndTime())
        .queryEqCompanyId(queryParam.getCompanyId())
        .queryEqDeviceId(queryParam.getDeviceId())
        .queryInEqDeviceId(queryParam.getDeviceIdList())
        .queryEqSimNo(queryParam.getSimNo())
        .queryEqAuthorizedStatus(queryParam.getAuthorizedStatus());
    return vehicleTboxParam;
  }
  /**
   * 3.1.1
   * 查询封装
   */
  private VehicleTbox getVehicleTboxBindingParam(
          QueryVehicleTboxListParamDto queryParam) {
    VehicleTbox vehicleTbox = new VehicleTbox();

    CommonUtils.copyProperties(queryParam, vehicleTbox);

    return vehicleTbox;
  }
}
